<template>
	<view class="contents">
		<view class="item" @click="openSchool">
			<view class="flex">
				<image src="../../../static/image/index/icon6.png" mode=""></image>
				<input disabled class="input font14" type="text" placeholder="请输入学校名称 " v-model="value" />
			</view>
		</view>
		<view class="item" @click="handlePopup1">
			<view class="flex" style="width: 86vw;">
				<image src="http://image.alhelp.net/uploads/20231104/49af34932aab1b4c6b36107032264077.png" mode=""></image>
				<view class="font14 texts">{{college||'请选择院系'}}</view>
			</view>
			<uni-icons type="bottom" size="14" style="margin-right: 20rpx;"></uni-icons>
		</view>
		<view class="item" @click="handlePopup">
			<view class="flex" style="width: 86vw;">
				<image src="../../../static/image/icons/zx.png" mode=""></image>
				<view class="font14 texts">{{speciality||'请选择专业'}}</view>
			</view>
			<uni-icons type="bottom" size="14" style="margin-right: 20rpx;"></uni-icons>
		</view>
		<view class="btn flex" @click="handleSure">
			<image src="http://image.alhelp.net/uploads/20231104/de726361ec2e01a130d793b0e5b48cb8.png" mode=""></image>
			<text>确认下载</text>
		</view>
		<u-popup v-model="popup" mode="bottom" :mask-close-able="false">
			<view class="popup-box">
				<view class="flex btns">
					<view class="font14 cancel" @click="handleCancel1">取消</view>
					<view class="font14 sure" @click="handleSure1">确认</view>
				</view>
				<scroll-view scroll-y="true" style="height: 600upx;" :show-scrollbar="false">
					<view class="popup-list" v-for="(item,i) in collegeList" :key="i">
						<view class="popup-list-item" @click="keep1(item)">
							<text class="texts">({{item.collegeCode}}){{item.collegeName}}</text>
						</view>
					</view>
				</scroll-view>
			</view>
		</u-popup>
		<u-popup mode="bottom" v-model="orderPopup" :mask-close-able="false">
			<view class="popup-box">
				<view class="flex btns">
					<view class="font14 cancel" @click="handleCancel">取消</view>
					<view class="font14 sure" @click="handleSure3">确认</view>
				</view>
				<scroll-view scroll-y="true" style="height: 600upx;" :show-scrollbar="false">
					<view class="popup-list" v-for="(item,i) in specialityList" :key="i">
						<view class="popup-list-item" @click="keep(item)">
							<text class="texts">({{item.majorCode}}){{item.majorName}}</text>
						</view>
					</view>
				</scroll-view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import {
		getSchoolList,
		getChooseSchool,
		getCollegeList,
		getChooseSchoolCode,
		getPDFList
		// isChooseSchool
	} from '@/api/toolbox.js'
	export default {
		data() {
			return {
				orderPopup: false,
				popup: false,
				scrollTop: 0,
				value: '',
				show: false,
				list: [],
				selectData: null,
				specialityList: [],
				speciality: '',
				college: '',
				collegeList: [],
				ispay: '',
				id: '',
				payOrderId: null, //订单ID
				chooseObj: {}

			};
		},
		onLoad(e) {
			if (e.payOrderId) {
				this.payOrderId = e.payOrderId;
			}
		},
		onShow() {
			if (uni.getStorageSync('chooseSchoolObj')) {
				let schoolObj = uni.getStorageSync('chooseSchoolObj');
				this.value = schoolObj.schoolName;
				uni.removeStorageSync('chooseSchoolObj');
			}
		},
		watch: {
			value() {
				this.college = ''
				this.speciality = ''
			},
			college() {
				this.speciality = ''
			}
		},
		methods: {
			openSchool() {
				uni.navigateTo({
					url: '/pages/toolbox/adjustReport/searchSchool',
				});
			},
			input(event) {
				this.value = event.target.value;
				this.getList();
			},
			async getList() {
				/** 输入框内有值就进行搜索，没有值就关闭 */
				if (this.value) {
					const res = await getSchoolList({
						schoolName: this.value
					})
					res.data.forEach(item => {
						this.list.push({
							name: item
						})
					})
					console.log(this.list)
					if (!this.show) this.show = true;
				} else {
					this.show = false;
				}
			},
			clonse() {
				this.show = false;
			},
			select(event) {
				this.show = false;
				this.value = event.name;
				this.selectData = JSON.stringify(event);
			},
			async handlePopup1() {
				if (!this.value) {
					uni.showToast({
						title: '请输入学校',
						icon: 'none'
					})
					this.college = ''
					this.speciality = ''
					return
				}
				const res = await getChooseSchool({
					schoolName: this.value
				})
				if (res.data.length > 0) {
					this.collegeList = res.data
					this.popup = true;
				} else {
					uni.showToast({
						title: '请正确输入学校',
						icon: 'none'
					})
				}
			},
			async handlePopup() {
				if (!this.value) {
					uni.showToast({
						title: '请输入学校',
						icon: 'none'
					})
					this.college = ''
					this.speciality = ''
					return
				}
				if (!this.college) {
					this.speciality = ''
					return uni.showToast({
						title: '请选择院系',
						icon: 'none'
					})
				}
				this.orderPopup = true;
				const res = await getCollegeList({
					schoolName: this.value,
					collegeName: this.college
				})
				this.specialityList = res.data
			},
			handleCancel() {
				this.orderPopup = false;
			},
			handleSure3() {
				this.orderPopup = false;
			},
			handleCancel1() {
				this.popup = false;
			},
			handleSure1() {
				this.popup = false;
			},
			keep1(item) {
				this.college = item.collegeName
				this.popup = false;
			},
			keep(item) {
				this.chooseObj = item;
				uni.setStorageSync('school', item)
				this.speciality = item.majorName
				this.id = item.id
				this.orderPopup = false;
			},
			async handleSure() {
				if (!this.value) {
					this.college = ''
					this.speciality = ''
					return uni.showToast({
						icon: 'none',
						title: '请输入大学'
					})
				}
				if (!this.college) {
					this.speciality = ''
					return uni.showToast({
						title: '请选择院系',
						icon: 'none'
					})
				}
				if (!this.speciality) {
					return uni.showToast({
						icon: 'none',
						title: '请选择专业'
					})
				}
				let data = {
					schoolName: this.chooseObj.schoolName,
					collegeName: this.chooseObj.collegeName,
					majorName: this.chooseObj.majorName,
					schoolCode: this.chooseObj.schoolCode,
					collegeCode: this.chooseObj.collegeCode,
					majorCode: this.chooseObj.majorCode,
					year: this.chooseObj.year,
					userId: uni.getStorageSync('userId'),
					orderId: this.payOrderId,
					reportId: 0,
					price: 0,
					page: 1,
					pageSize: 10,
					type: 1
				}
				const res = await getPDFList(data)
				uni.redirectTo({
					url: `/pages/toolbox/chooseSchool/download?id=${res.data}`
				})
			}
		}
	}
</script>

<style>
	page {
		background: #F6F8F6;
	}
</style>

<style lang="scss">
	.contents {
		padding: 30rpx;
		position: relative;
		height: 95vh;

		.item {
			width: 100%;
			height: 100rpx;
			background: #fff;
			border-radius: 12rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-bottom: 30rpx;

			image {
				width: 32rpx;
				height: 32rpx;
				margin: 0 15rpx 0 30rpx;
			}

			.texts {
				white-space: nowrap;
				overflow: hidden;
				text-overflow: ellipsis;
			}
		}

		.fuzzy {
			position: absolute;
			top: 140rpx;
			left: 100rpx;
		}

		.btn {
			position: absolute;
			bottom: 30rpx;
			left: 50%;
			transform: translate(-50%, 0);
			width: 400rpx;
			height: 96rpx;
			background: #F7DD5E;
			border-radius: 96rpx;
			justify-content: center;
			align-items: center;

			image {
				width: 40rpx;
				height: 40rpx;
				margin-right: 15rpx;
			}
		}
	}

	.popup-box {
		background: #fff;
		border-top-left-radius: 30rpx;
		border-top-right-radius: 30rpx;
		padding: 30rpx;

		.btns {
			align-items: center;
			justify-content: space-between;
			margin: 10rpx 0;

			.cancel {
				color: #AFAFAF;
			}

			.sure {
				color: #F7DD5E;
			}
		}

		.popup-list {
			.popup-list-item {
				text-align: center;
				height: 80upx;
				border-bottom: 2upx solid #F2F2F7;

				text {
					display: block;
					font-size: 28upx;
					color: #222E40;
					line-height: 80upx;
				}

				.texts {
					white-space: nowrap;
					overflow: hidden;
					text-overflow: ellipsis;
				}
			}
		}
	}
</style>